//
//  UserRegisterViewController.m
//  豆瓣(阿黄)
//
//  Created by dllo on 15/11/3.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserRegisterViewController.h"
#import "UserRegisterView.h"
#import "DataBase.h"

@interface UserRegisterViewController ()

@property (nonatomic, retain)UserRegisterView *name;

@property (nonatomic, retain)UserRegisterView *passWord;

@property (nonatomic, retain)UserRegisterView *confirmPass;

@property (nonatomic, retain)UserRegisterView *email;

@property (nonatomic, retain)UserRegisterView *phone;

@end

@implementation UserRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册";
    
    self.name = [[UserRegisterView alloc] initWithFrame:CGRectMake(0, 100, 375, 50)];
    self.name.Label.text = @"用户名:";
    self.name.textFiled.placeholder = @"请输入用户名";
    [self.view addSubview:self.name];
    [self.name release];
    
    self.passWord = [[UserRegisterView alloc] initWithFrame:CGRectMake(0, 160, 375, 50)];
    self.passWord.Label.text = @"密码:";
    self.passWord.textFiled.placeholder = @"请输入密码";
    [self.view addSubview:self.passWord];
    [self.passWord release];
    
    self.confirmPass = [[UserRegisterView alloc] initWithFrame:CGRectMake(0, 220, 375, 50)];
    self.confirmPass.Label.text = @"确认密码:";
    self.confirmPass.textFiled.placeholder = @"请输入密码";
    [self.view addSubview:self.confirmPass];
    [self.confirmPass release];
    
    self.email = [[UserRegisterView alloc] initWithFrame:CGRectMake(0, 280, 375, 50)];
    self.email.Label.text = @"邮箱:";
    self.email.textFiled.placeholder = @"请输入邮箱";
    [self.view addSubview:self.email];
    [self.email release];
    
    self.phone = [[UserRegisterView alloc] initWithFrame:CGRectMake(0, 340, 375, 50)];
    self.phone.Label.text = @"联系方式:";
    self.phone.textFiled.placeholder = @"请输入联系方式";
    [self.view addSubview:self.phone];
    [self.phone release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStyleDone target:self action:@selector(regAction:)];
    
}

- (void)regAction:(UIBarButtonItem *)btn{

    NSLog(@"1111");
    
    //插入数据库
    NSMutableArray *array = [[DataBase shareDataBase] selectUser:0];
    BOOL result = [array containsObject:self.name.textFiled.text];
    if (result == 0) {
        //    插入数据库
        NSString *insertSql = [NSString stringWithFormat:@"INSERT INTO user(name, pass) VALUES('%@', '%@')", self.name.textFiled.text, self.passWord.textFiled.text];
        [[DataBase shareDataBase] insertSql:insertSql];
        
        [self.navigationController popViewControllerAnimated:1];
        
    }

    
    if (self.name.textFiled.text.length == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"用户名不能为空" preferredStyle:1];
        [self presentViewController:alert animated:1 completion:^{
            
            
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
